﻿/******************************************************************************
 * 【本类功能概述】                                 					      *
 *  版权所有（C）2020-20XX，                                        *
 *  保留所有权利。                                                            *
 ******************************************************************************
 *  作者 : <hughjk>
 *  创建时间:2019/11/24 23:41:01
 *  文件描述: 
 *****************************************************************************/
using System;
using System.Collections.Generic;
using UnityEngine;
using Util;

namespace StateModel
{
    public class SoldierAttackState : SoldierState
    {
        public Weapon weapon;
        public float cd = 0.3f;
        private float zcd = 0;
        public float m_AlertneScope;
        private SoldierIdleState idleState;
        public override void InitModel(Controller controller)
        {
            base.InitModel(controller);
            stateType = StateType.Attack;
            weapon.InitModel(controller);
        }
        public override void GetModel()
        {
            base.GetModel();
            weapon.GetModel();
        }


        public override void OnUpdata(float dt)
        {
            base.OnUpdata(dt);
            if (IsObj(m_AlertneScope))//战斗
            {
                zcd += dt;
                if (zcd >= cd)
                {
                    zcd = 0;
                    weapon.transform.right = m_input.data.TagPos - weapon.transform.position;
                    weapon.Attack(m_input.data.TagPos);
                }
            }
            else
            {
                SwitchState(StateType.Idle);
            }

        }

        public bool IsObj(float dis)
        {
            RaycastHit2D[] hit = Physics2D.CircleCastAll(m_Pos, dis, Vector2.zero);
            debug.DrawCircle(m_Pos, dis);
            for (int i = 0; i < hit.Length; i++)
            {
                var go = hit[i].transform; if (go)
                {
                    var soldier = go.transform.GetComponent<Controller>();
                    if (soldier)
                    {
                        if (soldier._TeamType != m_controller._TeamType)
                        {
                            m_input.data.TagPos = go.transform.position + Vector3.up * 0.5f;
                            return true;
                        }
                    }
                }
            }
            return false;
        }
    }
}


